#include "GlRenderState.h"
#include "libExt\libExt.h"
namespace gengine
{
	namespace kernel
	{
		bool GlRenderState::EnableLighting        (bool enable) 
		{
			if(lighting == enable)
				return true;

			lighting = enable;
			return true;

		}

		bool  GlRenderState::EnableDephtTest       (bool enable) 
		{
			if(depthTest == enable)
				return true;
			
			if(enable)
				glEnable(GL_DEPTH_TEST);
			else
				glDisable(GL_DEPTH_TEST);

			depthTest = enable;
			return true;
		}

		bool GlRenderState::EnableAlphaTest       (bool enable) 
		{
			if(alphaTest == enable)
				return true;
			
			if(enable)
				glEnable(GL_ALPHA_TEST);
			else
				glDisable(GL_ALPHA_TEST);

			alphaTest = enable;
			return true;
		}

		bool  GlRenderState::EnableBlending        (bool enable) 
		{
			if(blending == enable)
				return true;
			
			if(enable)
				glEnable(GL_BLEND);
			else
				glDisable(GL_BLEND);

			blending = enable;
			return true;
		}

		bool GlRenderState::EnableStensilTest     (bool enable) 
		{
			if(stensilTest == enable)
				return true;
			
			if(enable)
				glEnable(GL_STENCIL_TEST);
			else
				glDisable(GL_STENCIL_TEST);

			stensilTest = enable;
			return true;
		}

		bool GlRenderState::EnableCulling         (bool enable) 
		{
			if(culling == enable)
				return true;
			
			if(enable)
				glEnable(GL_CULL_FACE);
			else
				glDisable(GL_CULL_FACE);

			culling = enable;
			return true;
		}

		void GlRenderState::BindTextureUnit     (int target,int id)
		{
			 glBindTexture(target, id);
		}
		void GlRenderState::UnbindTextureUnit     (int target)
		{
			glBindTexture(target, 0);
		}
		bool GlRenderState::EnableDepthWrites     (bool enabled) 
		{
			if(depthWrites == enabled)
				return true;

			if(enabled)
				glDepthMask(GL_TRUE);
			else
				glDepthMask(GL_FALSE);
	
			depthWrites = enabled;
			return true;
		}

		bool GlRenderState::SetCullFace            (int mode)   
		{
			if(cullFace == mode)
					return true;

				glCullFace(mode);
				cullFace = mode;
				return true;
		}

		bool GlRenderState::SetFrontFace           (int mode)    
		{
				if(frontFace == mode)
				return true;

				glFrontFace(mode);
				frontFace = mode;
				return true;
		}

		bool GlRenderState::SetPolygonModeFront    (int mode)   
		{
			if(polygonModeFront == mode)
			return true;

			glPolygonMode(GL_FRONT, mode);
			polygonModeFront = mode;
			return true;
		}

		bool GlRenderState::SetPolygonModeBack     (int mode)   
		{
			if(polygonModeBack == mode)
			return true;

			glPolygonMode(GL_BACK, mode);
			polygonModeBack = mode;
			return true;
		}

		bool GlRenderState::SetDepthFunc           (unsigned func)             
		{
			glDepthFunc(func);
	
			
			return true;
		}
		bool GlRenderState::SetAlphaFunc           (int alphaFunc, float ref ) 
		{
			glAlphaFunc(alphaFunc,ref);
	
	        return true;
	
		}
		bool GlRenderState::SetBlendFunc           (int srcFunc, int dstFunc)  
		{
			glBlendFunc(srcFunc,dstFunc);
	
			return true;
		}

		bool GlRenderState::SetStencilFunc         (int func, int ref, unsigned mask) 
		{
			glStencilFunc(func,ref,mask);
	
			
			return true;
		}
			
		bool GlRenderState::SetStencilOp           (int fail, int zfail, int zpass) 
			
		{
			glStencilOp(fail,zfail,zpass);
	
		
			return true;
		}
			
		bool GlRenderState::SetStencilWrites       (unsigned mask) 
			
		{
			glStencilMask(mask);
			
			return true;
		}
	}
}